package com.ruoyi.aitcommon.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.aitcommon.domain.AitDataMapping;
import com.ruoyi.aitcommon.mapper.AitDataMappingMapper;
import com.ruoyi.aitcommon.service.IAitDataMappingService;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.TokenService;

/**
 * 实体映射Service业务层处理
 * 
 * @author cli
 * @date 2021-06-19
 */
@Service
public class AitDataMappingServiceImpl implements IAitDataMappingService 
{
	private static final Logger log = LoggerFactory.getLogger(AitDataMappingServiceImpl.class);
    @Autowired
    private AitDataMappingMapper aitDataMappingMapper;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询实体映射
     * 
     * @param id 实体映射ID
     * @return 实体映射
     */
    @Override
    public AitDataMapping selectAitDataMappingById(Long id)
    {
        return aitDataMappingMapper.selectAitDataMappingById(id);
    }

    /**
     * 查询实体映射列表
     * 
     * @param aitDataMapping 实体映射
     * @return 实体映射
     */
    @Override
    public List<AitDataMapping> selectAitDataMappingList(AitDataMapping aitDataMapping)
    {
        return aitDataMappingMapper.selectAitDataMappingList(aitDataMapping);
    }

    /**
     * 新增实体映射
     * 
     * @param aitDataMapping 实体映射
     * @return 结果
     */
    @Override
    public int insertAitDataMapping(AitDataMapping aitDataMapping)
    {
        return aitDataMappingMapper.insertAitDataMapping(aitDataMapping);
    }

    /**
     * 修改实体映射
     * 
     * @param aitDataMapping 实体映射
     * @return 结果
     */
    @Override
    public int updateAitDataMapping(AitDataMapping aitDataMapping)
    {
        return aitDataMappingMapper.updateAitDataMapping(aitDataMapping);
    }

    /**
     * 批量删除实体映射
     * 
     * @param ids 需要删除的实体映射ID
     * @return 结果
     */
    @Override
    public int deleteAitDataMappingByIds(Long[] ids)
    {
        return aitDataMappingMapper.deleteAitDataMappingByIds(ids);
    }

    /**
     * 删除实体映射信息
     * 
     * @param id 实体映射ID
     * @return 结果
     */
    @Override
    public int deleteAitDataMappingById(Long id)
    {
        return aitDataMappingMapper.deleteAitDataMappingById(id);
    }

    
	@Override
	public String importAitDataMapping(List<AitDataMapping> maplist) {
		if (StringUtils.isNull(maplist) || maplist.size() == 0)
        {
            throw new CustomException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        
        for (AitDataMapping map : maplist)
        {
            try
            {
            	map.setCreateBy(operName);
            	this.insertAitDataMapping(map);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、映射 " + map.getCode() + " 导入成功");
                
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、映射 " + map.getCode() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
	}
}
